package nac.ncprmx;

import java.io.FileReader;
import java.io.IOException;
import java.nio.CharBuffer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;

@SuppressWarnings("serial")
public class NcprmxServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ChannelService channelService = ChannelServiceFactory.getChannelService();
		String token = channelService.createChannel("test");

		// Index is the contents of our index.html resource, details omitted for
		// brevity.
		FileReader reader = new FileReader("index.html");
	    CharBuffer buffer = CharBuffer.allocate(16384);
	    reader.read(buffer);
	    String index = new String(buffer.array());
		index = index.replaceAll("\\{\\{ token \\}\\}", token);

		resp.setContentType("text/html");
		resp.getWriter().write(index);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp){
		ChannelService channelService = ChannelServiceFactory.getChannelService();
		channelService.sendMessage(new ChannelMessage("test", "hey"));
	}
}
